<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/11/18
 * Time: 21:37
 */

namespace app\storage\controller;

use app\storage\model\StorageModel;
use app\storage\model\SupplierModel;
use cmf\controller\AdminBaseController;
use think\Request;

class StorageController extends AdminBaseController
{

    private $storageModel;
    use \app\storage\util\Common;

    public function __construct(StorageModel $storageModel,Request $request = null)
    {
        parent::__construct($request);
        $this->storageModel = $storageModel;
    }

    //仓库主页面
    public function index()
    {
        return $this->fetch('index');
    }

    //添加仓库
    public function add()
    {
        $supplierModel = new SupplierModel();
        $supplierInfo = $supplierModel->select();
        $this->assign('supplier',$supplierInfo);
        return $this->fetch('add');
    }

    //编辑仓库
    public function edit()
    {
        $where['uuid'] = input('storageUuid');
        $storageModel = new StorageModel();
        $storageInfo = $storageModel->with('SupplierStorage')->where($where)->find();
        $supplierModel = new SupplierModel();
        $supplierInfo = $supplierModel->select();
        $this->assign('storageInfo', $storageInfo);
        $this->assign('supplierInfo', $supplierInfo);
        return $this->fetch('edit');
    }

    /**
     * 获取仓库主页面展示信息
     */
    public function getStorage()
    {
        $where = $this->getWhere();
        $data = $this->storageModel->getStorage($where);
        return json($data);
    }

    /**
     * 添加仓库
     */
    public function addStorage()
    {
        $data = input();
        $this->storageModel->addStorage($data);
        $this->redirect('/storage/Storage/index');
    }

    /**
     * 删除仓库
     */
    public function deleteStorage()
    {
        $storageUuid = input('storageUuid');
        $storageInfo = StorageModel::get($storageUuid, 'Shelves')->toArray();
        if (empty($storageInfo['shelves_model'])) {
            $result = $this->storageModel->deleteStorage($storageUuid);
            $msg = (bool)$result === true ? '删除成功' : '删除失败';
            ajax_response((bool)$result, $msg);
        } else {
            ajax_response(false, '请先删除该仓库货架');
        }
    }

    /**
     * 编辑仓库
     */
    public function editStorage()
    {
        $data = input();
        $this->storageModel->editStorage($data);
        $this->redirect('/storage/Storage/index');
    }


    /**
     * 添加是判断仓库名是否重复
     */
    public function checkStorageName()
    {
        $name = input('name');
        $uuid = input('uuid');
        $whereAnd = empty($uuid)?'':"and uuid != '$uuid'";
        $result = $this->storageModel->where("binary name = '$name' $whereAnd")->find();
        ajax_return((bool)$result);
    }

    /**
     * 检查Ip是否可用
     */
    public function checkIP()
    {
        $ip = input('ip_address');
        $storageName = input('name');
        $result = $this->storageModel->where('ip_address', $ip)->find();

        $ret = false;
        if (!empty($result) && $result['name'] != $storageName) {
            $ret = true;
        }
        ajax_return($ret);
    }

}